package com.hxalex.simulationhandle.accessbilityService.receiver.wifi.service;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.accessibility.AccessibilityNodeInfo;

import com.hxalex.simulationhandle.accessbilityService.manger.WifiManger;
import com.hxalex.simulationhandle.accessbilityService.model.WifiInfoBean;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * Created by hx_Alex on 2018/6/3.
 */

public class SearchWifiService extends IntentService {
    
 public static ArrayBlockingQueue<AccessibilityNodeInfo> listSearchQueue=new ArrayBlockingQueue<AccessibilityNodeInfo>(1024);
 private ArrayList<WifiInfoBean> wifiInfoBeanArrayList;

    public SearchWifiService() {
        super("SearchWifiService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        while (true){
        if(listSearchQueue.isEmpty()) continue;
        WifiManger manger=WifiManger.getWifiManger(this);
        ArrayList<WifiInfoBean> connectedWifiList = manger.getConnectedWifiList();
            ArrayList<WifiInfoBean> wbList = manger.getWifiInfoBeanArrayList();
        wifiInfoBeanArrayList=new ArrayList<>();
        AccessibilityNodeInfo searchListInfo = listSearchQueue.poll();
     
        recycle(searchListInfo);
        //移除已连接或以尝试连接失败的wifi
        for(WifiInfoBean b:connectedWifiList)
        {
            for(int i=0;i<wifiInfoBeanArrayList.size();i++)
            {
                if(b.getWifiName().equals(wifiInfoBeanArrayList.get(i).getWifiName()))
                {
                    wifiInfoBeanArrayList.remove(i);
                }
            }
        }
        for(WifiInfoBean bean:wifiInfoBeanArrayList)
        {
            boolean isBreak=false;
            if(wbList==null || wbList.size()==0)
            {
                manger.setWifiInfoBeanArrayList(wifiInfoBeanArrayList);
                break;
            }
            for(WifiInfoBean bean1:wbList)
            {
                if(!bean.getWifiName().equals(bean1.getWifiName()))
                {
                    manger.setWifiInfoBeanArrayList(wifiInfoBeanArrayList);
                    isBreak=true;
                    break;
                }
            }
            if(isBreak) break;
        }
        
   
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void recycle(AccessibilityNodeInfo info) {
        if (info.getClassName().equals("android.widget.LinearLayout") && info.getViewIdResourceName() == null && info.isClickable()) {
        
            //wifi名称
            List<AccessibilityNodeInfo> accessibilityNodeInfosByViewId = info.findAccessibilityNodeInfosByViewId("com.snda.wifilocating:id/title");
            if (accessibilityNodeInfosByViewId != null && accessibilityNodeInfosByViewId.size() == 1) {
                String wifiName = accessibilityNodeInfosByViewId.get(0).getText() != null ? accessibilityNodeInfosByViewId.get(0).getText().toString() : "";
                wifiInfoBeanArrayList.add(new WifiInfoBean(info, wifiName));
            }
        } else {
            for (int i = 0; i < info.getChildCount(); i++) {
                if (info.getChild(i) != null) {
                    recycle(info.getChild(i));
                }
            }
        }
    }
}
